package com.wzh.musichome.utils;


/**
 * @program: gaiaworks-job
 * @description: 本地ehcache工具类
 * @author: Trevor.Wu
 * @create: 2020-7-31 17:15
 **/


import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhCacheUtil {


    public static CacheManager cacheManager = CacheManager.getInstance();

    //存入缓存
    public static void set(String cacheName,Object key,Object value){
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null){
            cache.put(new Element(key,value));
        }
    }

    //查缓存
    public static Object get(String cacheName, String key){
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null){
            Element element = cache.get(key);
            if (element != null){
                return element.getObjectValue();
            }
        }
        return null;
    }

    //移除缓存
    public  static  Boolean remove(String cacheName,Object key){
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null){
           return cache.remove(key);
        }
        return false;
    }

    //查看指定的cache中是否有对应的key
    public static  Boolean seeCache(String cacheName,Object key){
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null){
            return cache.isKeyInCache(key);
        }
        return false;
    }

}
